import Vue from "vue";
import VueRouter, { RouteConfig, Route } from "vue-router";

import CustomLayout from "@/views/CustomLayout.vue";
import { rNames, rPaths } from "@/config/menuConfig";
import { isUndefined } from "lodash";

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    name: rNames.login,
    path: rPaths.login,
    component: () => import(/* webpackChunkName: "login" */ "@/views/Login.vue")
  },
  {
    name: rNames.regist,
    path: rPaths.regist,
    component: () =>
      import(/* webpackChunkName: "regist" */ "@/views/Regist.vue")
  },
  {
    name: rNames.home,
    path: rPaths.home,
    component: CustomLayout,
    children: [
      {
        name: rNames.home,
        path: rPaths.home,
        component: () =>
          import(/* webpackChunkName: "dashboard" */ "@/views/Home.vue")
      }
    ]
  },
  {
    name: rNames.products,
    path: rPaths.products,
    component: CustomLayout,
    children: [
      {
        name: rNames.category,
        path: rPaths.category,
        component: () =>
          import(/* webpackChunkName: "category" */ "@/views/Category.vue")
      },
      {
        name: rNames.product,
        path: rPaths.product,
        meta: { keepAlive: true },
        component: () =>
          import(/* webpackChunkName: "product" */ "@/views/Product.vue")
      },
      {
        name: rNames.productAddUpdate,
        path: `${rPaths.productAddUpdate}/:data`,
        props: (route: Route) =>
          !isUndefined(route.params)
            ? {
                data: route.params.data
              }
            : "",
        meta: { flagAddToNavigation: false },
        component: () =>
          import(
            /* webpackChunkName: "product-add-update" */ "@/views/ProductAddUpdate.vue"
          )
      },
      {
        name: rNames.productDetail,
        path: rPaths.productDetail,
        meta: { flagAddToNavigation: false },
        component: () =>
          import(
            /* webpackChunkName: "product-detail" */ "@/views/ProductDetail.vue"
          )
      }
    ]
  },
  {
    name: rNames.pageManage,
    path: rPaths.pageManage,
    component: CustomLayout,
    children: [
      {
        name: rNames.go,
        path: rPaths.go,
        component: () => import(/* webpackChunkName: "go" */ "@/views/Go.vue")
      }
    ]
  },
  {
    name: rNames.messageBoard,
    path: rPaths.messageBoard,
    component: CustomLayout,
    children: [
      {
        name: rNames.messageBoard,
        path: rPaths.messageBoard,
        component: () =>
          import(
            /* webpackChunkName: "message-board" */ "@/views/MessageBoard.vue"
          )
      }
    ]
  },
  {
    name: rNames.askList,
    path: rPaths.askList,
    component: CustomLayout,
    meta: { keepAlive: true },
    children: [
      {
        name: rNames.askList,
        path: rPaths.askList,
        component: () =>
          import(/* webpackChunkName: "ask-list" */ "@/views/AskList.vue")
      }
    ]
  },
  {
    path: "*",
    redirect: { name: rNames.home }
  }
];

/**
 * 这里能够清除重复点击一个aside连接，显示同一个页面产生的错误
 */
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location: string) {
  return (originalPush.call(this, location) as any).catch((err: any) => err);
};

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

export default router;
